<?php
include("include/conn.php");
require('include/Smarty/libs/Smarty.class.php');

$fromDate = isset($_POST['fromDateYear']) ? $_POST['fromDateYear']."-".$_POST['fromDateMonth']."-".$_POST['fromDateDay'] : date("Y-m-d");
$toDate   = isset($_POST['toDateYear']) ? $_POST['toDateYear']."-".$_POST['toDateMonth']."-".$_POST['toDateDay'] : date("Y-m-d");
$rojmelDetailArray      = array();
$opening                = array();
$total                  = array();
$opening['particular']  = "Opening";
$opening['crFine']      = 0;
$opening['crAmount']    = 0;
$opening['cramount']    = 0;
$opening['drAmount']    = 0;
$opening['dramount']    = 0;
$total['drFine']        = 0;
$total['drfine']        = 0;
$total['crFine']        = 0;
$total['crfine']        = 0;
$total['crAmount']      = 0;
$total['drAmount']      = 0;
$finalOpening['fine']   = 0;
$finalOpening['amount'] = 0;
$finalFineOpeningDr     = 0;
$finalFineOpeningCr     = 0;
$finalAmountOpeningCr   = 0;
$finalAmountOpeningDr   = 0;
$currentRowDisplayAt    = 0;
$currentRowDisplayAt    = 0;
$netAmount              = 0;
$netFine                = 0;
$totalCrFine            = 0;
$totalCrAmount          = 0;
$totalDrFine            = 0;
$totalDrAmount          = 0;

// This for get data from rojmel : Start
$selectRojmel = "SELECT rojmelId,partyName, rojmelDate, particulars, fine, amount, CrDr 
                   FROM rojmel
                  WHERE rojmelDate <= '".$toDate."'
                    AND rojmelDate >= '".$fromDate."'
               ORDER BY rojmelDate";
$selectRojmelRes = mysql_query($selectRojmel);
$rojmelCount = 0;
$rojmelCrCount = 0;
$rojmelDrCount = 0;
$currentRowDisplayAt = 0;
$opening['drFine']      = 0;
while($rojmelRow = mysql_fetch_assoc($selectRojmelRes))
{
  if($rojmelRow['rojmelDate'] >= $fromDate)
  {
    if(isset($rojmelDetailArray[$currentRowDisplayAt]['rojmelDate']) && ($rojmelDetailArray[$currentRowDisplayAt]['rojmelDate']   == $rojmelRow['rojmelDate'] || $rojmelDetailArray[$currentRowDisplayAt]['rojmelDateCr'] == $rojmelRow['rojmelDate'] ))// && date prev and current both same
      $rojmelDetailArray[$currentRowDisplayAt]['displayGhat'] = 0;
    else
    {
      $currentDebitGhat = 0;	
      $myDrGroupIdStartRow = $rojmelCount;
  		$rojmelDrCount = 0;
  		$rojmelCrCount = 0;
    }
  	if($rojmelRow['CrDr'] == "Dr")
      $currentRowDisplayAt = $myDrGroupIdStartRow + $rojmelDrCount;
    else
      $currentRowDisplayAt = $myDrGroupIdStartRow + $rojmelCrCount;    

    if($rojmelRow['CrDr'] == 'Dr')
    {
  		if($rojmelDrCount >= $rojmelCrCount)
      {
        //For Credit Blank Vars : Start
  		  $rojmelDetailArray[$currentRowDisplayAt]['rojmelIdCr']      = 0;
  		  $rojmelDetailArray[$currentRowDisplayAt]['partyNameCr']     = "";
  		  $rojmelDetailArray[$currentRowDisplayAt]['rojmelDateCr']    = "";
  		  $rojmelDetailArray[$currentRowDisplayAt]['particularCr']    = "";
  		  $rojmelDetailArray[$currentRowDisplayAt]['fineDr']          = "";
  		  $rojmelDetailArray[$currentRowDisplayAt]['amountDr']        = "";
  		  //For Credit Blank Vars : End
  		}
      $rojmelDetailArray[$currentRowDisplayAt]['rojmelId']   = $rojmelRow['rojmelId'];
      $rojmelDetailArray[$currentRowDisplayAt]['partyName']  = $rojmelRow['partyName'];
      $rojmelDetailArray[$currentRowDisplayAt]['rojmelDate'] = $rojmelRow['rojmelDate'];
      $rojmelDetailArray[$currentRowDisplayAt]['particular'] = $rojmelRow['particulars'];
      $rojmelDetailArray[$currentRowDisplayAt]['fineDr']     = $rojmelRow['fine'];
      $rojmelDetailArray[$currentRowDisplayAt]['amountDr']   = $rojmelRow['amount'];
    }
    else
    {
    	if($rojmelCrCount >= $rojmelDrCount)
      {
  		  $rojmelDetailArray[$currentRowDisplayAt]['rojmelId']     =  0;
  		  $rojmelDetailArray[$currentRowDisplayAt]['partyName']    =  "";
  		  $rojmelDetailArray[$currentRowDisplayAt]['rojmelDate']   =  "";
  		  $rojmelDetailArray[$currentRowDisplayAt]['particular']   =  "";
  		  $rojmelDetailArray[$currentRowDisplayAt]['fineCr']       =  "";
  		  $rojmelDetailArray[$currentRowDisplayAt]['amountCr']     =  "";
  		} 
      $rojmelDetailArray[$currentRowDisplayAt]['rojmelIdCr']   = $rojmelRow['rojmelId'];
      $rojmelDetailArray[$currentRowDisplayAt]['partyNameCr']  = $rojmelRow['partyName'];
      $rojmelDetailArray[$currentRowDisplayAt]['rojmelDateCr'] = $rojmelRow['rojmelDate'];
      $rojmelDetailArray[$currentRowDisplayAt]['particularCr'] = $rojmelRow['particulars'];
      $rojmelDetailArray[$currentRowDisplayAt]['fineCr']       = $rojmelRow['fine'];;
      $rojmelDetailArray[$currentRowDisplayAt]['amountCr']     = $rojmelRow['amount'];
    } 
  }
    
    if($rojmelRow['CrDr'] == 'Dr')
    {  
      $total['drAmount']   += $rojmelRow['amount'];
      $total['drFine']     += $rojmelRow['fine'];
  		$rojmelDrCount++;
  		if($rojmelDrCount > $rojmelCrCount)
  		  $rojmelCount++;
  	}
    else
    {
      $total['crAmount'] += $rojmelRow['amount'];
      $total['crFine']   += $rojmelRow['fine'];
  	  $rojmelCrCount++;
  		if($rojmelCrCount > $rojmelDrCount)
  		  $rojmelCount++;
  	}
 
}

/// Find The Net Fine And Net Amount
$netAmount =  ($total['drAmount'] - $total['crAmount']);
$netFine   =  ($total['drFine']   - $total['crFine']);  
 

$selectRojmel = "SELECT rojmelId,partyName, rojmelDate, particulars, fine, amount, CrDr 
                   FROM rojmel
                  WHERE rojmelDate < '".$fromDate."'
               ORDER BY rojmelDate";
$selectRojmelRes = mysql_query($selectRojmel);
while($rojmelRow = mysql_fetch_assoc($selectRojmelRes))
{
    if($rojmelRow['CrDr'] == 'Cr')
    {
      $opening['crFine']   += $rojmelRow['fine'];
      $opening['crAmount'] += $rojmelRow['amount'];
    }
    else
    {
      $opening['drFine']   += $rojmelRow['fine'];
      $opening['drAmount'] += $rojmelRow['amount'];
    }
    
    
    $finalOpening['fine'] = $opening['drFine'] - $opening['crFine'];
  	$finalOpening['amount'] = $opening['drAmount'] - $opening['crAmount'];
  	
  	
  	if($finalOpening['fine'] > 0)
  	{
  	  $finalFineOpeningDr = $finalOpening['fine'];
  	  $finalFineOpeningCr = "";
    }          
    else
    {
      $finalFineOpeningCr = 0 - $finalOpening['fine'];
      $finalFineOpeningDr = "";    
    }
                    
    if($finalOpening['amount'] > 0)
  	{
  	  $finalAmountOpeningDr = $finalOpening['amount'];
  	  $finalAmountOpeningCr = "";
    }          
    else
    {
      $finalAmountOpeningCr = 0 - $finalOpening['amount'];
      $finalAmountOpeningDr = "";                     
    }
}

$netAmount += $finalOpening['amount'];
$netFine   += $finalOpening['fine'];
 
$finalAmountOpeningCr;
$finalAmountOpeningDr;
$finalFineOpeningCr;
$finalFineOpeningDr;

$total['drAmount'];
$total['drFine'];
$total['crAmount'];
$total['crFine'];

if($finalAmountOpeningCr != "")
{
  $totalCrAmount = $finalAmountOpeningCr + $total['crAmount'];
}

if($finalAmountOpeningDr != "")
{
  $totalDrAmount = $finalAmountOpeningDr + $total['drAmount'];
}

if($finalFineOpeningCr != "")
{
	$totalCrFine = $finalFineOpeningCr + $total['crFine'];
}

if($finalFineOpeningDr != "")
{
	$totalDrFine = $finalFineOpeningDr + $total['drFine'];
}

  
//print_r($rojmelDetailArray);
$smarty = new Smarty;
require("bottom.php");
$smarty->assign("l_credit",$l_credit);
$smarty->assign("l_debit",$l_debit);
$smarty->assign("l_date",$l_date);
$smarty->assign("l_particulars",$l_particulars);
$smarty->assign("l_fine",$l_fine);
$smarty->assign("l_amount",$l_amount);
$smarty->assign("l_from",$l_from);
$smarty->assign("l_to",$l_to);
$smarty->assign("l_find",$l_find);
$smarty->assign("l_total",$l_total);
$smarty->assign("l_netAmount",$l_netAmount);
$smarty->assign("l_netFine",$l_netFine);
$smarty->assign("l_delete",$l_delete);
$smarty->assign("l_edit",$l_edit);
$smarty->assign("fromDate",$fromDate);
$smarty->assign("toDate",$toDate);
$smarty->assign("rojmelDetailArray",$rojmelDetailArray);
$smarty->assign("opening",$opening);
$smarty->assign("total",$total);
$smarty->assign("netAmount",$netAmount);
$smarty->assign("netFine",$netFine);
$smarty->assign("totalCrFine",$totalCrFine);
$smarty->assign("totalCrAmount",$totalCrAmount);
$smarty->assign("totalDrFine",$totalDrFine);
$smarty->assign("totalDrAmount",$totalDrAmount);
$smarty->assign("finalFineOpeningDr",$finalFineOpeningDr);
$smarty->assign("finalFineOpeningCr",$finalFineOpeningCr);
$smarty->assign("finalAmountOpeningDr",$finalAmountOpeningDr);
$smarty->assign("finalAmountOpeningCr",$finalAmountOpeningCr);
$smarty->display("rojmel.tpl");
?>
